package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PmsProductCategoryMapper;
import com.itheima.pojo.PmsProductCategory;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class PmsProductCategoryImpl implements PmsProductCategoryApi {
    @Autowired
    private PmsProductCategoryMapper mapper;

    @Override
    public void save(PmsProductCategory category) {
        mapper.insert(category);
    }

    @Override
    public PmsProductCategory findById(Long id) {
        PmsProductCategory category = mapper.selectById(id);
        return category;
    }

    @Override
    public List<PmsProductCategory> findAll() {
        return mapper.selectList(new LambdaQueryWrapper<PmsProductCategory>());
    }

    @Override
    public void remove(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public List<PmsProductCategory> findByLevel(int i) {
        List<PmsProductCategory> categories
                = mapper.selectList(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getLevel, i));

        return categories;
    }

    @Override
    public List<PmsProductCategory> findByPid(Long pid) {
        List<PmsProductCategory> categories
                = mapper.selectList(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getParentId, pid));

        return categories;
    }

    @Override
    public PageResultVo getPages(Long pid, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        Page<PmsProductCategory> selectPage
                = mapper.selectPage(page, new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getParentId, pid));
        PageResultVo vo = new PageResultVo();
        vo.setTotalPages((int) selectPage.getPages());
        vo.setList(selectPage.getRecords());
        vo.setTotal((int) selectPage.getTotal());
        vo.setPageNum(pageNum);
        vo.setPageSize(pageSize);
        return vo;
    }

    @Override
    public void updateNavStatus(Integer ids, Integer navStatus) {

        PmsProductCategory category = new PmsProductCategory();
        category.setNavStatus(navStatus);
        mapper.update(category, new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getId, ids));
    }

    @Override
    public void updateShowStatus(Integer ids, Integer showStatus) {
        PmsProductCategory category = new PmsProductCategory();
        category.setShowStatus(showStatus);
        mapper.update(category, new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getId, ids));

    }

    @Override
    public void updateById(Long id, PmsProductCategory category) {
        mapper.update(category,
                new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getId, id));
    }

    @Override
    public PmsProductCategory getByid(Long id) {
      return   mapper.selectById(id);

    }

}
